package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.annotation.SysLog;
import com.platform.cache.ShopLoginCacheUtil;
import com.platform.entity.SysUserVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiSysUserServiceImpl;
import com.platform.service.SysUserRoleService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.*;
import com.platform.validator.Assert;
import com.platform.validator.ValidatorUtils;
import com.platform.validator.group.AddGroup;
import com.platform.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * 系统用户
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2016年10月31日 上午10:40:10
 */
@Api(tags = "系统操作员")
@RestController
@RequestMapping("/api/sysuser")
public class ApiSysUserController extends ApiBaseAction {
    @Autowired
    private ApiSysUserServiceImpl apiSysUserService;

    /**
     * 微信端 保存用户
     */
//    @SysLog("保存用户")
    @ApiOperation(value = "添加系统用户")
    @PostMapping("save")
    public Object save(@LoginUser UserVo loginUser,
                       String username,
                       String mobile,
                       Long deptId) {
//        ValidatorUtils.validateEntity(user, AddGroup.class);

//        JSONObject jsonParam = getJsonRequest();
        SysUserVo sysUserVo = new SysUserVo();
//        sysUserVo.setUsername(jsonParam.getString("username"));
//        sysUserVo.setMobile(jsonParam.getString("mobile"));
//        sysUserVo.setDeptId(jsonParam.getLong("deptId"));
        sysUserVo.setUsername(username);
        sysUserVo.setMobile(mobile);
        sysUserVo.setDeptId(deptId);
        sysUserVo.setStatus(1);
        sysUserVo.setCreateTime(new Date());
        sysUserVo.setCustomerId(loginUser.getUserId());
        apiSysUserService.save(sysUserVo);

        //缓存sysUser对象
        ShopLoginCacheUtil.set( ""+sysUserVo.getCustomerId(),sysUserVo);

        return toResponsSuccess("新增成功");
    }

}
